package com.youdianstar.app.util

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.youdianstar.app.R
import com.youdianstar.app.adapter.MerchantPOPAdapter
import com.youdianstar.app.entity.MerchantOP


/**
 * File: AcPopupUtil
 * Author: 82149 Create: 2024/2/3 18:30
 * Changes (from 2024/2/3)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class AcPopupUtil {
    companion object {

        val instance by lazy { AcPopupUtil() }

    }

    /**
     * 显示操作选择
     *
     * @param listData
     */
    fun showOPPopupWindow(
        context: Activity, parentView: View, widthP: Int = -1,
        listData: List<MerchantOP>, selectAction: ((Int) -> Unit) = { },
    ) : PopupWindow {
        val rootView: View = LayoutInflater.from(context).inflate(R.layout.layout_spring_container, null)
        val rcyList = rootView.findViewById<RecyclerView>(R.id.spring_data)

        val withSet :Int = if (widthP == -1) ViewGroup.LayoutParams.MATCH_PARENT else widthP
        val window = PopupWindow(rootView,withSet, ViewGroup.LayoutParams.WRAP_CONTENT)
        window.setBackgroundDrawable(ContextCompat.getDrawable(context,R.drawable.shape_bg_trans))
        window.isOutsideTouchable = true
        window.isFocusable = true

        rcyList.layoutManager = LinearLayoutManager(context)
        val adapter = MerchantPOPAdapter(context,listData,object : MerchantPOPAdapter.IListItemListener {
            override fun onSelectedItem(p: Int) {
                window.dismiss()
                selectAction.invoke(p)
            }
        })
        rcyList.adapter = adapter

        window.showAsDropDown(parentView)
        return window
    }
}